package com.qf.team2.netdisc.service.impl;

import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.common.pojo.entity.Link;
import com.qf.team2.netdisc.dao.DocumentMapper;
import com.qf.team2.netdisc.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author
 */
@Service
public class DocumentServiceImpl implements DocumentService {
    @Autowired
    private DocumentMapper documentMapper;

    @Override
    public String checkDocument(String url) {
        System.out.println("service");
        System.out.println(url);
        Link link = documentMapper.checkDocument(url);
        if (link == null) {
            throw new RuntimeException("资源不存在");
        }

        String verifyCode = link.getVerifyCode();
        String md5 = link.getMd5();
        if (verifyCode == null || "".equals(verifyCode)) {
            return "http://localhost:8080/views/download.html?md5=" + md5;
        }

        return "有验证码";

    }

    @Override
    public String checkCode(String url, String code) {
        Link link = documentMapper.checkDocument(url);
        String verifyCode = link.getVerifyCode();
        if (code.equals(verifyCode)) {
            String md5 = link.getMd5();
            String downloadUrl = "http://localhost:8080/netdisc/views/view-api/download.html?md5=" + md5;
            return downloadUrl;
        }
        return "";
    }

    @Override
    public Document getDocumentById(Integer id) {

        return documentMapper.getDocumentById(id);
    }

}
